package rus.webapp.action;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.myfaces.custom.fileupload.UploadedFile;

import rus.model.ContentImage;
import rus.model.ProductCategory;
import rus.service.ContentImageManager;
import rus.service.ProductCategoryManager;

public class ProductCategoryForm extends BasePage implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6038764752973549883L;
	
	
	private ProductCategoryManager productCategoryManager;
	private ContentImageManager contentImageManager;
	private ProductCategory productCategory = new ProductCategory();
	private List<ProductCategory> productCategoriesList = new ArrayList<ProductCategory>();
	private UploadedFile file;
	private String id;
	
	public ProductCategoryForm(){
		setSortColumn("name");
	}
	
	public String callAddNewProduct(){
		log.info("Call crating new product category");
		productCategory = new ProductCategory(); 
		return "success";
	}
	
	public String addNewProduct() throws IOException{		
		ContentImage contentImage = null;
		byte[] byteArray = null;
		
		if(getFile() != null){
		contentImage =	new ContentImage();
		
		InputStream stream = file.getInputStream();
		ByteArrayOutputStream bAOut = new ByteArrayOutputStream();
		int c;
		while ((c = stream.read()) != -1) {
			bAOut.write((char) c);
		}
			byteArray = bAOut.toByteArray();
			contentImage.setURI(StringUtils.replace(StringUtils.lowerCase(productCategory.getName()), " ", "/"));
			contentImage.setData(byteArray);
			contentImageManager.saveContentImage(contentImage);
		}			
		
		log.info("Create product category");
		productCategory.setImage(contentImage);
		productCategoryManager.saveProductCategory(productCategory);
		return "success";
	}
	
	public String callEditProductCategory(){
		setProductCategory(productCategoryManager.getProductCategoryById(Integer.parseInt(id))); 
		return EDIT;
	}
	
	public String editProductCategory() throws IOException{
		ContentImage contentImage = null;
		byte[] byteArray = null;
		
		if(getFile() != null){
		contentImage =	new ContentImage();
		
		InputStream stream = file.getInputStream();
		ByteArrayOutputStream bAOut = new ByteArrayOutputStream();
		int c;
		while ((c = stream.read()) != -1) {
			bAOut.write((char) c);
		}
			byteArray = bAOut.toByteArray();
			contentImage.setURI(StringUtils.replace(StringUtils.lowerCase(productCategory.getName()), " ", "/"));
			contentImage.setData(byteArray);
			contentImageManager.saveContentImage(contentImage);
		}			
		
		log.info("Create product category");
		productCategory.setImage(contentImage);
		productCategoryManager.saveProductCategory(productCategory);
		return SUCCESS;

	}
	
	public String cancel(){
		log.info("Back to product categories list");
		return CANCEL; 
	}
	
	public List<ProductCategory> getProductCategoryList(){
		return productCategoryManager.getList();	
	}
	

	public void setProductCategory(ProductCategory productCategory) {
		this.productCategory = productCategory;
	}

	public ProductCategory getProductCategory() {
		return productCategory;
	}

	public void setProductCategoryManager(ProductCategoryManager productCategoryManager) {
		this.productCategoryManager = productCategoryManager;
	}

	public ProductCategoryManager getProductCategoryManager() {
		return productCategoryManager;
	}

	public void setProductCategoriesList(List<ProductCategory> productCategoriesList) {
		this.productCategoriesList = productCategoriesList;
	}

	public List<ProductCategory> getProductCategoriesList() {
		return productCategoriesList;
	}

	public void setContentImageManager(ContentImageManager contentImageManager) {
		this.contentImageManager = contentImageManager;
	}

	public ContentImageManager getContentImageManager() {
		return contentImageManager;
	}

	public void setFile(UploadedFile file) {
		this.file = file;
	}

	public UploadedFile getFile() {
		return file;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}


}
